bosangong = fk.CreateSkill {
    name = "bosangong",
    tags = { Skill.Compulsory },
}

-- 技能翻译表
Fk:loadTranslationTable{
    ["bosangong"] = "三公",
    [":bosangong"] = "当你获得其他角色的手牌后，或当其他角色获得你的手牌后，你可以将手牌摸至场上最多的手牌数。",
    ["#bosangong-invoke"] = "三公：将手牌摸至场上最多的手牌数",
    ["$bosangong1"] = "掌中握青锋，汝剑利，吾剑未尝不利！",
    ["$bosangong2"] = "今上并无失德，汝欲废嫡立庶，非反而何？",
}

-- 监听卡牌移动事件
bosangong:addEffect(fk.AfterCardsMove, {
    anim_type = "draw",
    can_trigger = function(self, event, target, player, data)
        -- 检查是否拥有技能
        if not player:hasSkill(bosangong.name) then
            return false
        end
        
        -- 检查是否有手牌转移事件
        local hasHandCardTransfer = false
        for _, move in ipairs(data) do
            -- 自己获得其他角色的手牌
            if move.to == player and move.from and move.from ~= player and move.toArea == Card.PlayerHand then
                for _, info in ipairs(move.moveInfo) do
                    if info.fromArea == Card.PlayerHand then
                        hasHandCardTransfer = true
                        break
                    end
                end
            -- 其他角色获得自己的手牌
            elseif move.from == player and move.to and move.to ~= player and move.toArea == Card.PlayerHand then
                for _, info in ipairs(move.moveInfo) do
                    if info.fromArea == Card.PlayerHand then
                        hasHandCardTransfer = true
                        break
                    end
                end
            end
            if hasHandCardTransfer then break end
        end
        
        return hasHandCardTransfer
    end,
    
    on_cost = function(self, event, target, player, data)
        local room = player.room
        -- 询问是否发动技能
        return room:askToSkillInvoke(player, {
            skill_name = bosangong.name,
            prompt = "#bosangong-invoke"
        })
    end,
    
    on_use = function(self, event, target, player, data)
        local room = player.room
        
        -- 计算场上最多的手牌数
        local maxCards = 0
        local players = room:getAllPlayers()
        
        for _, p in ipairs(players) do
            if not p.dead then
                local handcardNum = p:getHandcardNum()
                if handcardNum > maxCards then
                    maxCards = handcardNum
                end
            end
        end
        
        -- 如果自己的手牌数小于最多的手牌数，则摸牌
        local currentCards = player:getHandcardNum()
        if currentCards < maxCards then
            local drawNum = maxCards - currentCards
            player:drawCards(drawNum, bosangong.name)
            room:notifySkillInvoked(player, bosangong.name)
        end
    end,
})

return bosangong
